package com.fish.anne.webapp.util;

import java.util.Random;

public class McCodeCrcUtil {

	public static String code() {
		Random _random = new Random();
		
		//^ (((long) request.hashCode()) << 32) 
		
		long r0 =   Runtime.getRuntime().freeMemory() ^ _random.nextInt() ;
		long r1 = _random.nextLong();
		if (r0 < 0)
			r0 = -r0;
		if (r1 < 0)
			r1 = -r1;
		String id = Long.toString(r0) + Long.toString(r1);
		//Long.parseLong(s, radix)
		
		if(id.length()< 10){
			id = id + (System.currentTimeMillis()>>4);
		}
		 
		return id;
	}
	
	public static final int[] factor = {4,6,7,3,2,5,2,9,7,8};
	
	public static String crc(String code){
		int[] factor1 = {7,9,17,3,11,31,63,91,61,29};
		char[] cc = code.toCharArray();
		int sum = 0;
		for(int i =0 ;i<factor.length;i++){
			sum += (int)cc[i] *factor[i]*factor1[i];
		}
		sum = sum % 79;
		if(sum<10){
			return "0"+sum;
		}
		return String.valueOf(sum);
	}
	
	public static String crcCode(){
		String code =code();
		return code+crc(code);
	}
	
	
	public static boolean crcCheck(String code){
		//int len = factor.length;
		String crc = crc(code);
		
		String rawCrc = code.substring(code.length()-2);
		if(rawCrc.equals(crc)){
			return true;
		}
		return false;
	}
	
	
	public static void main(String[] args){
		
		 String code = "507721110121212951019467208220";
		 
		  
		 
		 System.out.print(crcCheck(code));
		 
		
	}
}
